<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Room;
use App\Admin\Repositories\User;
use Dcat\Admin\Form;

use Dcat\Admin\Grid;
use Dcat\Admin\Widgets\Checkbox;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Widgets\Radio;

class UserController extends AdminController
{

    public function index(Content $content)
    {
        return $content->header('用户管理')
            ->description('信息')
            ->body(
                $this->grid()
            );
    }
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new User('room'), function (Grid $grid) {
            $grid->column('id');
            $grid->column('nick_name')->editable(true);
            $grid->column('login_name');
            $grid->column('photo')->image('', 100, 100);
            $grid->room()->pluck('name')->label();
            $grid->column('is_online');
            $grid->column('is_robot');
            $grid->column('sex')->display(function ($sex) {
                if ( $sex == 1 ) {
                    return "男";
                } elseif ( $sex == 2 ) {
                    return "女";
                } else {
                    return "无";
                }
            });
            $grid->column('email');
            $grid->column('status');
            $grid->column('create_time')->sortable();
            $grid->column('update_time')->sortable();

            $grid->model()->orderBy('id', 'desc');
            // 开启字段选择器功能
            $grid->showColumnSelector();
            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('login_name');
                $filter->equal('nick_name');

            });
        });
    }

    public function show($id, Content $content): Content
    {
        return $content->header('用户管理')
            ->description('详情')
            ->body(
                $this->detail($id)
            );


    }
    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail(mixed $id): Show
    {
        return Show::make($id, new User(), function (Show $show) {
            $this->extracted($show);
            $show->field('create_time');
            $show->field('update_time');
        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new User('roomUser'), function (Form $form) {

            $table = $form->repository()->model()->getTable();
            $connection = config('admin.database.connection');
            $id = $form->getKey();
            $form->text('nick_name')->required()->minLength(3);
            $form->text('login_name')->required()->creationRules(
                ['required', "unique:{$connection}.{$table}"],
                [
                    "required" => trans('user.fields.login_name') . "必填",
                    "unique" => trans('user.fields.login_name') . "唯一"
                ]
            )->updateRules(
                ['required', "unique:{$connection}.{$table},login_name,$id"],
                [
                    "required" => trans('user.fields.login_name') . "必填",
                    "unique" => trans('user.fields.login_name') . "唯一"
                ]

            )->width(6);

            $form->password('password', trans('admin.password'))
                ->minLength(6)
                ->maxLength(20)
                ->customFormat(function ($v) {
                    if ( $v == $this->password ) {
                        return;
                    }

                    return $v;
                });
            $form->password('password_confirmation', trans('admin.password_confirmation'))->same('password');
            $form->ignore(['password_confirmation']); // 忽略字段
            $form->image('photo')->autoUpload()->required();
            $form->radio('sex')->options(
                [
                    "无", "男", "女"
                ]
            );
            $form->checkbox("room_ids", "房间")->options(

                array_column((new Room())->getList(), "name", "id")

            );

            $form->email('email')->required();
            $form->datetime('update_time')->readOnly();
            $form->submitted(
                function ($form) {
                    if ( $form->isCreating() ) {
                        $form->create_time = time();
                    }
                    $form->update_time = time();
                }
            );
            $form->saving(function (Form $form) {
                if ( $form->password && $form->model()->get('password') != $form->password ) {
                    $form->password = md5($form->password);
                }

                if ( !$form->password ) {
                    $form->deleteInput('password');
                }

            });

            $form->saved(function (Form $form) {

                return $form
                    ->response()
                    ->success(trans('admin.update_succeeded'))
                    ->redirect('im/user');
            });


        });
    }

    /**
     * @param Show $show
     * @return void
     */
    protected function extracted(Show $show): void
    {
        $show->field('id');
        $show->field('nick_name');
        $show->field('login_name');
        $show->field('photo')->image();
        $show->field('is_online');
        $show->field('is_robot');
        $show->field('sex')->unescape()->as(function ($sex) {
            $name = "sex";
            $options = [
                "无",
                "男",
                "女"
            ];

            $radio = Radio::make($name, $options); // 选中第一个选项
            $radio->check($sex);
            return $radio->render();
        });
        $show->field('email');
        $show->field('status');
    }

}
